//1. 引入 mongoose
const mongoose = require('mongoose');

//2. 连接数据库
let result = mongoose.connect('mongodb://127.0.0.1:27017/h5211227');

//3. 调用 then 方法
result.then(v => {
    //4. 创建文档结构对象
    let StudentSchema = new mongoose.Schema({
        name: String,
        age: Number,
        gender: String
    });

    //5. 创建模型对象
    let StudentModel = mongoose.model('student', StudentSchema);

    //6. 数据操作
    let res = StudentModel.create({name: '胡凯', age: 20, gender: '男'})

    //写法一
    // res.then(v => {
    //     console.log(v, '成功');
    // }, r => {
    //     console.log(r, '失败');
    // })

    //写法二
    return res;
}, r => {
    console.log('连接失败!!!');
}).then(v => {
    console.log(v, 'success');
});